﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;
using ABCat.Core;
using ABCat.Core.Plugins;
using ABCat.UI.WPF.Models;
using ABCat.UI.WPF.UI;
using Microsoft.Win32;

namespace ABCat.UI.WPF
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow
	{
		#region + Fields +

		private Context _context;

		#endregion
		#region + Ctor +

		public MainWindow()
		{
			InitializeComponent();
			_context = new Context(App.Logger, ConfigViewModel.ShowConfigWindow, RecordTargetDownloaderViewModel.ShowLoginWindow);

			this.Loaded += MainWindow_Loaded;

#if DEBUG
			this.AddHandler(Window.KeyDownEvent, new KeyEventHandler((e, w) =>
			{
				if (w.Key == System.Windows.Input.Key.F12)
				{
					var sfd = new SaveFileDialog();
					sfd.RestoreDirectory = true;
					sfd.DefaultExt = ".png";
					sfd.AddExtension = true;

					if (sfd.ShowDialog().Value)
					{
						File.WriteAllBytes(sfd.FileName, this.ToBitmap());
					}
				}
			}), true);

#endif

		}


		#endregion
		#region + Logic +

		protected override void OnClosing(CancelEventArgs e)
		{
			base.OnClosing(e);
			CatalogViewerUc.StoreLayouts();
			Saveable.SaveAll();
			Application.Current.Shutdown();
		}

		private void MainWindow_Loaded(object sender, RoutedEventArgs e)
		{
			if (!DesignerProperties.GetIsInDesignMode(this))
			{
				CatalogViewerUc.Init(_context);
				CatalogViewerUc.RestoreLayouts();
			}
		}

		#endregion
	}
}